package com.sz.app.oss.api.pay.account;

import com.sz.app.oss.exception.ErrorCodes;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.constants.MessagesCodes;
import com.sz.biz.pay.account.dto.PayBankCardDto;
import com.sz.biz.pay.account.entity.BankCard;
import com.sz.biz.pay.account.service.AccountService;
import com.sz.biz.pay.account.service.BankCardService;
import com.sz.biz.pay.account.service.BankInfoService;
import com.sz.biz.pay.account.service.ThirdPayAccountService;
import com.sz.biz.pay.constants.PayConstant;
import com.sz.common.base.cache.redis.RedisPoolManager;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.i18n.I18nMessageHelper;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.pay.pingan.constant.UnionPayBank;
import com.sz.common.pay.pingan.constant.UnionPayBankData;
import com.sz.common.pay.pingan.entity.b2b.query.TimePeriodOpenRemoveAccountDetailsRequest;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.List;


/**
 * Function: TODO: ADD FUNCTION <br>
 *
 * @author: 陈航旭 <br>
 * @date: 2017-12-01 18:14:00
 */
@RestController
@RequestMapping(value = "/api/v1/pay/account")
@Api(description = " ", tags = "050、资金账户管理")
public class PayAccountController extends BaseController {
    @Autowired
    AccountService accountService;
    @Autowired
    ThirdPayAccountService thirdPayAccountService;
    @Autowired
    BankCardService bankCardService;
    @Autowired
    private BankInfoService bankInfoService;


    @ApiOperation(value = "查询账户信息", notes = "查询账户信息")
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public ResultDto findAccountInfo() {
        return thirdPayAccountService.findAccountInfo(PayConstant.PaymentType.PINGAN);

    }

    @ApiOperation(value = "查询账户余额信息", notes = "查询账户余额信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thirdPayAccountId", value = "第三方账户id",
                    required = true, dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "/balance/{thirdPayAccountId}", method = {RequestMethod.GET})
    public ResultDto finAccountBlanceInfo(@PathVariable String thirdPayAccountId) {
        return thirdPayAccountService.findAccountBalance(thirdPayAccountId);
    }


    @ApiOperation(value = "查询最新添加的银行卡信息", notes = "查询最新添加的银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thirdPayAccountId", value = "第三方账户id",
                    required = true, dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "/newest/card/{thirdPayAccountId}", method = {RequestMethod.GET})
    //查询最新添加银行卡信息
    public ResultDto findNewestCardInfo(@PathVariable String thirdPayAccountId) {
        PayBankCardDto payBankCardDto = bankCardService.findNewestCard(thirdPayAccountId);
        if (null == payBankCardDto) {
            throw Exceptions.bizException(ErrorCodes.ERROR_PAY_BANKCARD_NOT_EXISTS);
        }
        ResultDto result = new ResultDto();
        result.setData(payBankCardDto);
        return result;
    }

    @ApiOperation(value = "查询所有银行卡信息", notes = "查询所有银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thirdPayAccountId", value = "第三方账户id",
                    required = true, dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "all/card/{thirdPayAccountId}", method = {RequestMethod.GET})
    //查询所有银行卡信息
    public ResultDto findAllCardInfo(@PathVariable String thirdPayAccountId) {
        //根据第三方账户ID查询所有银行卡信息
        List<PayBankCardDto> payBankCardDtos = bankCardService.findAllByThirdPayAccountId(thirdPayAccountId);
        ResultDto result = new ResultDto();
        result.setData(payBankCardDtos);
        return result;
    }

    @ApiOperation(value = "绑定银行卡", notes = "绑定银行卡")
    @RequestMapping(value = "bind/card", method = {RequestMethod.POST})
    //绑定银行卡
    public ResultDto bindCard(@RequestBody @ApiParam(value = "要保存的数据", required = true) @Validated PayBankCardDto payBankCardDto) {
        return bankCardService.bindAccountCard(payBankCardDto);

    }

    @ApiOperation(value = "验证金额", notes = "验证金额")
    @RequestMapping(value = "verify/amount", method = {RequestMethod.POST})
    //验证金额
    public ResultDto verifyAmount(@RequestBody @ApiParam(value = "验证绑定的数据", required = true) @Validated PayBankCardDto payBankCardDto) {
        return bankCardService.verifyBindCard(payBankCardDto);

    }

    @ApiOperation(value = "解绑银行卡", notes = "解绑银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankCardId", value = "银行卡id",
                    required = true, dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "unbind/card/{bankCardId}", method = {RequestMethod.GET})
    //解绑银行卡
    public ResultDto unbindAccountCard(@PathVariable String bankCardId) {
        return bankCardService.unbindAccountCard(bankCardId);

    }

    @ApiOperation(value = "绑卡-短信发送验证码接口", notes = "绑卡-短信发送验证码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobilePhone", value = "手机号码",
                    required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/sendsmscode", method = {RequestMethod.GET})
    public ResultDto sendSmsValiateCode(@RequestParam String mobilePhone) {
        ResultDto result = new ResultDto();
        boolean isSend = bankCardService.sendSms(mobilePhone);
        result.setMsg(I18nMessageHelper.getI18nMessage(MessagesCodes.MODULE_PAY_SMS_CODE_SEND_SUCCESS, null));
        result.setData(isSend);
        return result;
    }

    /**
     * 检查验证码是否正确
     */
    @ApiOperation(value = "检查短信验证码", notes = "检查短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNo", value = "手机号码",
                    required = true, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "validateCode", value = "验证码", required = true, dataType = "String", paramType = "query")})
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    public ResultDto checkCode(@RequestParam String validateCode, String phoneNo) {
        ResultDto result = new ResultDto();
        Jedis jedis = RedisPoolManager.getJedis();
        String cacheValidateCode = jedis.get(phoneNo + "bindCard");

        if (validateCode.equals(cacheValidateCode)) {
            result.setData(true);
            result.setMsg(I18nMessageHelper.getI18nMessage(MessagesCodes.MODULE_PAY_SSMS_CODE_RIGHT, null));
        } else {
            result.setData(false);
            result.setErrorCode(ErrorCodes.ERROR_PAY_SMS_CODE_ERROR.getCode());
            result.setMsg(I18nMessageHelper.getI18nMessage(ErrorCodes.ERROR_PAY_SMS_CODE_ERROR.getMessageKey(), null));
        }
        jedis.close();
        return result;
    }

    @ApiOperation(value = "获取开户行列表", notes = "获取开户行列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字",
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段",
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序类型,true为升序，false为降序", required = false,
                    dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页 默认第一页",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "bankClsCode", value = "行别代码",
                    dataType = "string", paramType = "query")})
    @RequestMapping(value = "/usernamelist", method = {RequestMethod.GET})
    public QResultDto getUserNameList(@RequestParam(defaultValue = "") String q,
                                      @RequestParam(defaultValue = "1") int pageIndex,
                                      @RequestParam(defaultValue = "10") int pageSize,
                                      @RequestParam(defaultValue = "") String sort,
                                      @RequestParam(defaultValue = "true") boolean isAsc,
                                      @RequestParam(defaultValue = "")
                                              String bankClsCode
    ) throws BizException {
        q = StringUtils.replaceSqlPattern(q);
        return bankInfoService.findBankName(q, pageIndex, pageSize, sort, isAsc, null, bankClsCode);

    }

    @ApiOperation(value = "查询银行列表", notes = "查询银行列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId", value = "账户id",
                    required = true, dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "banklist", method = {RequestMethod.GET})
    public List<UnionPayBank> findUnionPayBank() {
        return UnionPayBankData.getUnionPayBanks();
    }

    @ApiOperation(value = "开通账户", notes = "开通账户")
    @RequestMapping(value = "open/account/{accountId}", method = {RequestMethod.GET})
    public ResultDto openAccount(@PathVariable String accountId) {
        return thirdPayAccountService.openAccount(accountId);
    }

    @ApiOperation(value = "查询已验证银行卡列表", notes = "查询已验证银行卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thirdPayAccountId", value = "第三方账户id",
                    required = true, dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "verify/bankcard/{thirdPayAccountId}", method = {RequestMethod.GET})
    public ResultDto getVerifiedCard(@PathVariable String thirdPayAccountId) {
        ResultDto result = new ResultDto();
        List<BankCard> bankCards = bankCardService.getVerifiedCard(thirdPayAccountId);
        result.setData(bankCards);
        return result;
    }


    @ApiOperation(value = "判断银行卡是否存在", notes = "判断银行卡是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardNo", value = "银行卡号",
                    required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/checkcard", method = {RequestMethod.GET})
    public ResultDto checkCardExist(@RequestParam String cardNo) {
        return bankCardService.checkCardExist(cardNo);
    }

    @ApiOperation(value = "查时间段会员开销户信息", notes = "查时间段会员开销户信息")
    @RequestMapping(value = "/account/details", method = {RequestMethod.GET})
    public ResultDto findOpenRemoveAccountDetails() {
        TimePeriodOpenRemoveAccountDetailsRequest timePeriodOpenRemoveAccountDetailsRequest =
                new TimePeriodOpenRemoveAccountDetailsRequest();
        return thirdPayAccountService.findOpenRemoveAccountDetails(timePeriodOpenRemoveAccountDetailsRequest);
    }


    @ApiOperation(value = "展示账户基本信息", notes = "展示账户基本信息")
    @RequestMapping(value = "/baseinfo", method = {RequestMethod.GET})
    public ResultDto findAccountBaseInfo() {
        return thirdPayAccountService.findAccountBaseInfo(PayConstant.PaymentType.PINGAN);
    }

    @ApiOperation(value = "展示账户余额信息", notes = "展示账户余额信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thirdPayAccountId", value = "第三方账户id",
                    required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/amount", method = {RequestMethod.GET})
    public ResultDto findAmount(@RequestParam String thirdPayAccountId){
        return thirdPayAccountService.getMemberBalance(thirdPayAccountId);
    }

    @ApiOperation(value = "展示银行卡信息", notes = "展示银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thirdPayAccountId", value = "第三方账户id",
                    required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/bankcardinfo", method = {RequestMethod.GET})
    public ResultDto showBankCardInfo(@RequestParam String thirdPayAccountId){
        return bankCardService.getBankInfo(thirdPayAccountId);
    }

}
